package com.irdstudio.efp.nls.service.yed.common.rules.apply;

import com.irdstudio.basic.framework.core.constant.EsbRetCodeStandard;
import com.irdstudio.basic.framework.core.exception.BizException;
import com.irdstudio.efp.nls.service.yed.common.rules.CheckRule;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 需求编号:2019D08--
 * 问题编号:
 * 开发人员: caoxin
 * 创建日期:2020/1/17
 * 功能描述:
 * 修改日期:2020/1/17
 * 修改描述:
 */
public class RepayMethodAndLoanTermRule implements CheckRule {
    private static Logger logger = LoggerFactory.getLogger(RepayMethodAndLoanTermRule.class);
    private final String FB01 = "FB01";
    private final String FB02 = "FB02";
    private final String FB03 = "FB03";

    public RepayMethodAndLoanTermRule(String rpymtMd, String rpyLoanTerm) {
        this.rpymtMd = rpymtMd;
        this.rpyLoanTerm = rpyLoanTerm;
    }

    private Map<String, List<String>> rpMdLoanTermMap = new HashMap<String, List<String>>();
    {
        rpMdLoanTermMap.put(FB01, Stream.of("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36").collect(Collectors.toList()));
        rpMdLoanTermMap.put(FB02, Stream.of("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36").collect(Collectors.toList()));
        rpMdLoanTermMap.put(FB03, Stream.of("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36").collect(Collectors.toList()));
    }

    // 还款方式
    private String rpymtMd;
    // 贷款期限
    private String rpyLoanTerm;


    @Override
    public void check() throws BizException {
        if (StringUtils.isEmpty(rpymtMd) || StringUtils.isEmpty(rpyLoanTerm)){
            throw new BizException(EsbRetCodeStandard.WFJKYWGZ.getValue(), "还款方式为空或还款周期为空，请核查！");
        }
        List<String> rpyLoanTerms = rpMdLoanTermMap.get(rpymtMd);
        if (Objects.isNull(rpyLoanTerms)) {
            throw new BizException(EsbRetCodeStandard.WFJKYWGZ.getValue(), "不支持该种还款方式，请核查！");
        }
        for (String loanTerm : rpyLoanTerms) {
            if (loanTerm.equals(rpyLoanTerm)) {
                return;
            }
        }
        throw new BizException(EsbRetCodeStandard.WFJKYWGZ.getValue(), "6个月以内可选先息后本(FB01)、等额本息(FB03)；6-12个月等额本息(FB03)，可随借随还(FB08)，请核查！");

    }
}
